package server.provider;

import com.hz.ironc.cloud.thritf.api.UserService;
import org.springframework.stereotype.Component;
import server.conf.ZooKeeperConfig;

import java.util.Map;
import java.util.Random;

/**
 * Created by tfw2302 on 2017/1/19.
 */
@Component
public class UserServiceProvider {

    public UserService.Client getBalanceUserService(){
        // 以负载均衡的方式获取服务是咧
        Map<String, UserService.Client> serviceMap = ZooKeeperConfig.serviceMap;
        //以负载均衡的方式获取服务实例
        for (Map.Entry<String, UserService.Client> entry : serviceMap.entrySet()) {
            System.out.println("可供选择服务:"+entry.getKey());
        }
        int rand=new Random().nextInt(serviceMap.size());
        String[] mkeys = serviceMap.keySet().toArray(new String[serviceMap.size()]);
        return serviceMap.get(mkeys[rand]);
    }

}
